<?php

/*
 * This file is part of the Geek-Zoo Projects.
 *
 * @copyright (c) 2010 Geek-Zoo Projects More info http://www.geek-zoo.com
 * @license http://opensource.org/licenses/gpl-2.0.php The GNU General Public License
 * @author quqiang <quqiang@geek-zoo.com>
 *
 */


require ROOT_PATH.'/Helper/admin_abstract.php';

class Action extends admin_abstract
{
    public function index()
    {
			$status = _GET('recycle', 0);
	
            $q = _GET('q');

    	    if(addslashes($q)){

    		    $like = " and content LIKE '%$q%' ";
    	    }else{

    		    $like = '';
	    }
	     $t=_GET('t'); 
	    if(!$t ){
	    	$t = 'desc';
	    }
	    if($t=='DESC'){
	    	$t = 'asc';
	    }
	    if($t=='ASC'){
	    	$t = 'desc';
	    }
    	$getNewlist = _model('comment')->getList("where status='$status' ".$like.'order by add_time '.$t, new Pager(20));
        $this->view->assign($getNewlist);
        $this->view->display("admin/comment_list.html");
    }
    public function getUserrevielwList($user_id)
    {
        $user_id=_GET('user_id');
        if(!empty($user_id))
        {
        $getUserrevielwList=_model("comment")->getlist("where userid = {$user_id},order by id desc", new Pager(20));
        $this->view->assign("$getUserrevielwList");
        $this->view->display("");
        }
    }
    public function getPostidrevielwRead($post_id)
    {
        $post_id=_GET("id");
        if($post_id)
        {
            $anwser=_model("comment")->getList("where id = {$post_id}");
        }else
        {
            $this->msg("没有ID");
        }
    }
    public function updateRevielw()
    {
        if(Request::isPost())
        {
            $newarray=array();
            $newRevielw=_POST();
            $flt = array('content');
            foreach ($flt as  $value) {
                $newarray[$value]=$newRevielw[$value];
            }
            $id=_POST('id');
            $anwser=_model("comment")->update(array("id"=>$id), $newarray);
            if($anwser)
            {
                $this->msg("更新成功", "admin/comment_manage");
            }
            else
            {
                $this->msg("添加失败");
            }
        }
    }
    public function revielwRead()
    {
        $id=_GET("id", 0);
        if(!empty($id))
        {
        $newContent=_model("comment")->read(array("id"=>$id));
        $this->view->assign("data", $newContent);
        $this->view->display("admin/comment_update.html");
        }else
        {
            $this->msg("没有用户ID");
        }
    }
    // public function delRevielw()
    // { 
    //         if(Request::isPost())
    //         {
    //                 $id=array_filter(_POST("id", array()));
    // 					if(empty($id)){
    // 						$this->msg("请传入ID");
    // 					}
    //                 $newId=implode(",", $id);
    //                 $anwser=_model("comment")->delete("where id in ({$newId})");
    //         }else{
    //             $id=_GET("id", 0);
    //             $anwser=_model("comment")->delete(array("id"=>$id));               
    //         }
    //             $this->redirect("admin/comment_manage");   
    // }


    public function delRevielw()
    {   
				if(Request::isPost())
				{
				   $id=array_filter(_POST("id", array()));
						if ($id) {
							foreach ($id as $key => $value) {
							     $anwser=_model("comment")->update(array('id'=>$value), array('status'=>'1'));
									$info = _model('comment')->read(array('id'=>$value));
									$post = _model('post')->read(array('id'=>$info['post_id']));
									_model('post')->update(array('id'=>$info['post_id']), array('comment_num'=>$post['comment_num']-1));
							}
						}
				}else{
				   $id=_GET("id", 0);
					 if ($id) {
					   $anwser=_model("comment")->update(array("id"=>$id), array('status'=>'1'));
							$info = _model('comment')->read(array('id'=>$id));
							$post = _model('post')->read(array('id'=>$info['post_id']));
							_model('post')->update(array('id'=>$info['post_id']), array('comment_num'=>$post['comment_num']-1));
					 }
				}
				$this->redirect("admin/comment_manage");
    }


		function redelete()
		{
			$id = _GET('id', 0);
			if ($id) {
				 _model("comment")->update(array("id"=>$id), array('status'=>'0'));
					$info = _model('comment')->read(array('id'=>$id));
					$post = _model('post')->read(array('id'=>$info['post_id']));
					
					_model('post')->update(array('id'=>$info['post_id']), array('comment_num'=>$post['comment_num']+1));
			}
			$this->redirect("admin/comment_manage?recycle=1");
      
		}
		
		function truedelete()
		{
       if(Request::isPost())
         {
             $id=_POST("id", array());
             $newId=implode(",", $id);
             $anwser=_model("comment")->delete("where id in ({$newId})");
         }else{
             $id=_GET("id", 0);
             $anwser=_model("comment")->delete(array("id"=>$id));               
         }
         $this->redirect("admin/comment_manage?recycle=1");
		}





    private function getTime()
    {
        return time();
    }
}
?>
